//
//  ZMJumpHelper.swift
//  TravelForUser
//
//  Created by Rowling on 2020/9/9.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import UIKit

class ZMJumpHelper: NSObject {
    class func destroy(VC:UIViewController,classes: [AnyClass],_ notLast : Bool = false) {
        guard let temp = VC.navigationController?.viewControllers else {
            return
        }
        var result = [UIViewController]()
        
        for i in 0 ..< temp.count {
            let vc = temp[i]
            if !classes.contains(where: {$0==vc.classForCoder}) || (notLast && (i == temp.count - 1))  {
                result.append(vc)
            }
        }
//        for vc in temp {
//            if !classes.contains(where: {$0==vc.classForCoder}) || (notLast && )  {
//                result.append(vc)
//            }
//        }
        VC.navigationController?.viewControllers = result
    }
    
    /// 销毁当前到目标控制器之间的控制器
    class func destroy(FromVC:UIViewController,toFirstClass:AnyClass,isContainSelf:Bool=false,isContainFirst:Bool=false,except:[AnyClass]=[AnyClass]()) {
        guard let temp = FromVC.navigationController?.viewControllers else {
            return
        }
        var result = [UIViewController]()
        var isbegin = false
        for vc in temp {
            if !isbegin {
                result.append(vc)
            } else {
                if except.contains(where: {$0 == vc.classForCoder}) {
                    result.append(vc)
                }
            }
            if vc.classForCoder == toFirstClass {
                if isContainFirst {
                    result.removeLast()
                }
                isbegin = true
            }
            if vc == FromVC {
               isbegin = false
                if !isContainSelf {
                    result.append(vc)
                }
            }
        }
        FromVC.navigationController?.viewControllers = result
    }
    
    class func jumpToClass(VC:UIViewController, toClass:AnyClass,notContainAutoJump : Bool) -> Bool {
        guard let nav = VC.navigationController else {
            return false 
        }
        let vcs = nav.children
        var cureentVC : UIViewController?
        for v in vcs {
            if v.classForCoder == toClass {
                cureentVC = v
            }
        }
        if let cv = cureentVC {
            nav.popToViewController(cv, animated: true)
            return true
        }
        else if notContainAutoJump {
            nav.popViewController(animated: true)
            // 不包含
            return false
        } else {
            // 不包含
            return false
        }
    }

}
